package com.eb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eb.annotation.AutoAnnouncement;
import com.eb.annotation.AutoCalculateExp;
import com.eb.context.BaseContext;
import com.eb.domain.Likes;
import com.eb.domain.Look;
import com.eb.enumeration.OperationType;
import com.eb.exception.BaseException;
import com.eb.mapper.ILikesMapper;
import com.eb.result.Result;
import com.eb.service.LikesService;
import com.eb.service.LookService;
import com.eb.vo.LikeInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

@Service
public class LikesServiceImpl extends ServiceImpl<ILikesMapper, Likes> implements LikesService {
    @Autowired
    private ILikesMapper likesMapper;
    @Autowired
    private LookService lookService;
    /**
     * 统计帖子的点赞数量
     * @param innovationId
     * @return
     */
    @Override
    public Long countLikesByInnovationId(Integer innovationId) {
        //利用count方法统计帖子的点赞数
        //创建QueryWrapper对象构建查询条件
        QueryWrapper<Likes> wrapper = new QueryWrapper<>();
        //添加查询条件
        wrapper.eq("innovation_id",innovationId);
        //统计帖子的点赞数
        return likesMapper.selectCount(wrapper);
    }

    /**
     * 添加点赞
     * @param innovationId
     * @return
     */
    @Override
    @AutoAnnouncement(OperationType.LIKE)
    @AutoCalculateExp(OperationType.LIKE)
    public LikeInfoVO addLike(Integer innovationId){
        //必须浏览之后才能点赞
        lookService.addLook(innovationId);
        LikeInfoVO likeInfoVO = new LikeInfoVO();
        //创建QueryWrapper对象构建查询条件
        QueryWrapper<Likes> wrapper = new QueryWrapper<>();
        //添加查询条件
        wrapper.eq("innovation_id",innovationId);
        wrapper.eq("user_id",BaseContext.getCurrentId());
        //查询
        Likes likes = likesMapper.selectOne(wrapper);
        if(likes!=null){
            //如果点赞表里有就取消点赞
            return deleteLike(innovationId);
        }else{
            //没有就添加
            //创建一个新的Likes对象
            likes = new Likes();
            likes.setInnovationId(innovationId);
            likes.setUserId(BaseContext.getCurrentId());
            likes.setCreateTime(LocalDateTime.now());
            likesMapper.insert(likes);
            likeInfoVO.setMsg("点赞成功");
            likeInfoVO.setCount(countLikesByInnovationId(innovationId));
            //返回点赞数并提示点赞成功
            return likeInfoVO;
        }
    }

    /**
     * 取消点赞
     * @param innovationId
     * @return
     */
    @Override
    public LikeInfoVO deleteLike(Integer innovationId){
        LikeInfoVO likeInfoVO = new LikeInfoVO();
        //创建QueryWrapper对象构建查询条件
        QueryWrapper<Likes> wrapper = new QueryWrapper<>();
        //添加查询条件
        wrapper.eq("innovation_id",innovationId);
        wrapper.eq("user_id",BaseContext.getCurrentId());
        //查询
        Likes likes = likesMapper.selectOne(wrapper);
        if(likes!=null){
            //删除
            likesMapper.deleteById(likes.getId());
            likeInfoVO.setMsg("取消点赞成功");
            likeInfoVO.setCount(countLikesByInnovationId(innovationId));
        }
        return likeInfoVO;
    }

}
